﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
//using MixScript.DBService;
using MixScript.Appl.Session;

namespace MixScript.BLL.ApplAdmin
{
    //public class cApplAdmin:cDBService
    public class cApplAdmin : cBaseBLL
    {
        private cApplSession oSession;

      
        public string userID
        {
            get { return oSession.userID; }
            
        }
        public List<string> MemberGroupID
        {
            get { return oSession.MemberGroupID; }
 
        }
        public string UserLevel
        {
            get { return oSession.UserLevel; }
 
        }
        public string UserEmployeeID
        {
            get { return oSession.UserEmployeeID; }
 
        }
        public string UserDepartmentID
        {
            get { return oSession.UserDepartmentID; }        
        }

        public cApplAdmin()
        {
            if (oSession == null)
            {
                oSession = new cApplSession();                
            }
        }


        public bool savePriv(DataTable dt, string username, string groupmenu)
        {
            try
            {
                int nret=0;
                if (Dbserver.openConn())
                {
                    string ssql = "delete sy_secmatrix where groupid='" + username + "' and menuid in (select nodeid from sy_nodemenu where modulname='" + groupmenu + "')";
                    Dbserver.ExecuteNonQuery(ssql);
                    DataTable dtdest=new DataTable();
                    getTableStructure(ref dtdest, "sy_secmatrix");

                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr["tick"].ToString() == "1")
                        {
                            DataRow drr = dtdest.NewRow();
                            drr["menuid"] = dr["nodeid"];
                            drr["groupid"] = username;
                            drr[2] = "1";
                            drr[3] = "1";
                            drr[4] = "1";
                            drr[5] = "1";

                            dtdest.Rows.Add(drr);
                        }
                    }

                    string s="";
                     nret=    Dbserver.SqlUpdate(dtdest, "sy_secmatrix", out s);
                    
                    Dbserver.CommitAndCloseConn();

                }
                return nret > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public bool getPriv(ref DataTable dt, string username,string groupmenu)
        {
            try
            {
                string ssql = "exec getpriv @username='" + username + "', @modulname='"+groupmenu+"'";
                dt= new DataTable();
                dt=Dbserver.ExecuteQuery(ssql, "tbPrev");
                return dt.Rows.Count > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}
